const {
	Service
} = require('uni-cloud-router')
const {
	typeOf
} = require("lg-utils")
const db = uniCloud.database();
const fundColl = db.collection("fund-base");

module.exports = class CommonService extends Service {
		async searchFundBaseInfo(params) {
			let builder = {};
			const dbCmd = db.command; 
			// 传入是字符串 
			if (typeOf(params, "string,number")) {
				const props = ["code","name","abbr_name","spell_name"];
				builder = dbCmd.or(props.map( pi => { 
					return {
						[pi]:new RegExp(`.*${params}.*`, "gi")
					}
				}))
			}
			// 传入对象
			if (typeOf(params, "object")){
					for (const [prop, val] of Object.entries(params)) {
						builder[prop] = new RegExp(`.*${val}.*`, "gi");
					}
				}
				return await fundColl.where(builder).limit(10).get();
			}
			async removeAllFundBase() {
				const dbCmd = db.command;
				return await fundColl.where({
					_id: dbCmd.exists(true)
				}).remove();
			}


		}
